package com.mhfelipe.sandbox.prevayler.context;

import com.mhfelipe.sandbox.prevayler.prevalence.Empresa;
import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: mhfelipe
 * Date: 2/3/12
 * Time: 9:28 AM
 * To change this template use File | Settings | File Templates.
 */
public class PrevaylerContext {

    private static Prevayler prevayler;

    private PrevaylerContext() {
        super();
    }

    public static Prevayler getInstance() {
        if (prevayler == null) {
            PrevaylerFactory factory = new PrevaylerFactory();
            factory.configurePrevalentSystem(new Empresa());
            factory.configurePrevalenceDirectory("data");
            factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT);
            try {
                prevayler = factory.create();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Não foi possível criar o ambiente prevalente.", e);
            }
        }

        return prevayler;
    }
}
